package com.bkgy.modular.cwTargetSpeed.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.PageReadListener;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bkgy.modular.cwTargetSpeed.entity.CWTargetSpeed;
import com.bkgy.modular.cwTargetSpeed.service.CWTargetSpeedService;
import com.bkgy.modular.tbPdiQualityRecordC642.entity.EntidElsx;
import com.bkgy.sys.common.entity.ResultData;
import com.bkgy.util.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author
 * @version 1.0
 * @ClassName CWTargetSpeedController
 * @date 2023/6/1 10:22
 */
@RestController
@RequestMapping("/linaTuiTargetSpeed")
public class CWTargetSpeedController {

    @Autowired
    private CWTargetSpeedService cwTargetSpeedService;

    @PostMapping("/findByData")
    public ResultData findByData(@RequestBody JSONObject jsonObject) {
        return cwTargetSpeedService.findByData(jsonObject);
    }

    @PostMapping("/updateByData")
    public ResultData updateByData(@RequestBody JSONObject jsonObject) {
        return cwTargetSpeedService.updateByData(jsonObject);
    }
    /**
     * 列表导出功能
     * @param jsonObject
     * @param response
     */
    @PostMapping("/exportXlsx")
    public void export(@RequestBody JSONObject jsonObject, HttpServletResponse response) {
        cwTargetSpeedService.export(jsonObject,response);
    }


    @PostMapping("/importXlsx")
    public ResultData importXlsx(@RequestParam("file") MultipartFile multipartFile){
        if (multipartFile.isEmpty()) {
            return null;
        }
        List<CWTargetSpeed> list = new ArrayList<>();
        // 这里 需要指定读用哪个class去读，然后读取第一个sheet 文件流会自动关闭
        // 这里每次会读取3000条数据 然后返回过来 直接调用使用数据就行
        try {
            EasyExcel.read(multipartFile.getInputStream(), EntidElsx.class, new PageReadListener<EntidElsx>(dataList -> {
                List<String> collect = dataList.stream().map(EntidElsx::getEntid).collect(Collectors.toList());
                LambdaQueryWrapper<CWTargetSpeed> wrapper = new LambdaQueryWrapper<>();
                wrapper.in(CWTargetSpeed::getEntid,collect);
                List<CWTargetSpeed> list1 = cwTargetSpeedService.list(wrapper);

                if(!CollectionUtils.isEmpty(list1)){
                    list.addAll(list1);
                }
            })).sheet().doRead();
        } catch (Exception e) {
            e.printStackTrace();
        }
//        ArrayList<LedgerPlan> ledgerPlanArrayList = list.stream().collect(Collectors.collectingAndThen(
//                Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(LedgerPlan::getEntid))), ArrayList::new));
        return ResultData.ResultDataSuccess(list, list.size());
    }


    @PostMapping("/exportXlsxNew")
    public void exportNew(@RequestBody JSONObject json,HttpServletResponse response){
        List<String> entids = json.getJSONArray("entids").toJavaList(String.class);
        LambdaQueryWrapper<CWTargetSpeed> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(CWTargetSpeed::getEntid,entids);
        List<CWTargetSpeed> exportData = cwTargetSpeedService.list(wrapper);
        ExcelUtils.downloadExcel("连退日评价表",response,CWTargetSpeed.class,exportData);
    }
}
